package com.zm.demo.httpclient;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.concurrent.atomic.AtomicBoolean;

@Component
public class MailService {

    @Autowired
    private JavaMailSender sender;

    @Value("${spring.mail.username}")
    private String from;

    private LocalDateTime lastAlarmTime;

    /**
     * 发送预警邮件
     */
    public void sendMsg(Map<String, Map<String, Object>> map, Map<String, Map<String, Object>> tdsqlMap, Map<String,Map<String, Object>> tdsqlSlowSqlMap, String startTime, String endTime) {
//        if (lastAlarmTime != null && !LocalDateTime.now().plusMinutes(-15).isAfter(lastAlarmTime)){
//            System.out.println("15分钟之内触发过预警，不做提示！");
//            return;
//        }
        SimpleMailMessage message = new SimpleMailMessage();
        // 发送者邮箱
        message.setFrom(from);
        // 接收者邮箱
        message.setTo(new String[]{"zouming1@powersi.com,345434645@qq.com"});
        // 主题
        message.setSubject("告警邮件");
        String content = getContent(map, tdsqlMap, tdsqlSlowSqlMap, startTime, endTime);
        if (content == null){
            System.out.println("没有异常情况，不需要告警...");
        }else {
            // 邮件内容
            message.setText(content);
            System.out.println("告警邮件开始...");
//            sender.send(message);
            try {
                SendMailService.send(content);
            } catch (Exception e){
                e.printStackTrace();
            }
            System.out.println("告警邮件已发送...");
            lastAlarmTime = LocalDateTime.now();
        }
    }

    private String getContent(Map<String, Map<String, Object>> map, Map<String, Map<String, Object>> tdsqlMap, Map<String,Map<String, Object>> tdsqlSlowSqlMap, String startTime, String endTime) {
        StringBuilder sb = new StringBuilder();
        AtomicBoolean atomicBoolean = new AtomicBoolean(false);
        sb.append(String.format("<h2>时间段： %s - %s </h2>", startTime, endTime));
        sb.append("<h3>TSF-接口调用监控：</h3>");
        map.values().stream().forEach(stringObjectMap -> {
            if (stringObjectMap.get("alarm") != null && (Boolean)stringObjectMap.get("alarm")){
                atomicBoolean.set(true);
            }
            Optional.ofNullable(stringObjectMap.get("desc")).ifPresent(desc -> sb.append("<br/>").append(desc));
        });
        sb.append("<h3>TDSQL-DB监控：</h3>");
        tdsqlMap.values().stream().forEach(stringObjectMap -> {
            if (stringObjectMap.get("alarm") != null && (Boolean)stringObjectMap.get("alarm")){
                atomicBoolean.set(true);
            }
            Optional.ofNullable(stringObjectMap.get("desc")).ifPresent(desc -> sb.append(desc).append("<br/><br/>"));
        });

        sb.append("<h3>TDSQL-慢SQL：</h3>");
        tdsqlSlowSqlMap.values().stream().filter(Objects::nonNull).forEach(stringObjectMap -> {
            if (stringObjectMap.get("alarm") != null && (Boolean)stringObjectMap.get("alarm")){
                atomicBoolean.set(true);
            }
            Optional.ofNullable(stringObjectMap.get("desc")).ifPresent(desc -> sb.append(desc).append("<br/><br/>"));
        });

        return atomicBoolean.get() ? sb.toString() : null;
    }

}